﻿using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Windows;
using MC.Phone.Ads.Provider;
using MC.Phone.Ads.Utils;
using MobFox.Ads;
using MobFoxAdControl = MobFox.Ads.AdControl;

namespace MC.Phone.Ads.MobFox
{
    public class MobFoxAdProvider : BaseAdProvider
    {
        public static readonly DependencyProperty PublisherProperty =
            DependencyProperty.Register("Publisher", typeof (string), typeof (MobFoxAdProvider), null);

        private WeakEventListener<MobFoxAdProvider, object, AdEngagedEventArgs> _adEngagedListener;
        private WeakEventListener<MobFoxAdProvider, object, NewAdEventArgs> _newAdListener;
        private WeakEventListener<MobFoxAdProvider, object, NoAdEventArgs> _noAdListener;

        [Category("Ad")]
        public string Publisher
        {
            get { return (string) GetValue(PublisherProperty); }
            set { SetValue(PublisherProperty, value); }
        }

        public override string Name
        {
            get { return "MobFox"; }
        }

        /// <summary>
        /// Not supported by MobFox.
        /// </summary>
        public override string CountryCode { set; protected get; }

        public override FrameworkElement ProvideAd()
        {
            var control = new MobFoxAdControl
                                    {
                                        PublisherID = Publisher,
                                        VerticalAlignment = VerticalAlignment.Bottom,
                                        TestMode = IsTest,
                                    };
            _adEngagedListener = new WeakEventListener<MobFoxAdProvider, object, AdEngagedEventArgs>(this)
                                     {
                                         OnEventAction = (instance, source, args) => instance.ControlAdEngaged(),
                                         OnDetachAction = weak => control.AdEngaged -= weak.OnEvent
                                     };
            control.AdEngaged += _adEngagedListener.OnEvent;
            _newAdListener = new WeakEventListener<MobFoxAdProvider, object, NewAdEventArgs>(this)
                                 {
                                     OnEventAction = (instance, source, args) => instance.ControlNewAd(),
                                     OnDetachAction = weak => control.NewAd -= weak.OnEvent
                                 };
            control.NewAd += _newAdListener.OnEvent;
            _noAdListener = new WeakEventListener<MobFoxAdProvider, object, NoAdEventArgs>(this)
                                {
                                    OnEventAction = (instance, source, args) => instance.ControlNoAd(),
                                    OnDetachAction = weak => control.NoAd -= weak.OnEvent
                                };
            control.NoAd += _noAdListener.OnEvent;

            return control;
        }

        public override void Clean()
        {
            _adEngagedListener = _adEngagedListener.DetachToNull();
            _newAdListener = _newAdListener.DetachToNull();
            _noAdListener = _noAdListener.DetachToNull();
        }

        private void ControlNoAd()
        {
            OnNoAd();
        }

        private void ControlNewAd()
        {
            OnNewAd();
        }

        private void ControlAdEngaged()
        {
            OnAdEngaged();
        }
        
#if DEBUG
        ~MobFoxAdProvider()
        {
            Debug.WriteLine( Thread.CurrentThread.ManagedThreadId + "--Finalizing " + GetType().FullName);
        }
#endif
    }
}